﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using CoreFrame.Common;
using MarchEstate.Manager.Dao.Cash;
using System.Data;
using MarchEstate.Manager.BusinesEntity.Costcalculate;
using MarchEstate.Manager.BusinesEntity;

namespace MarchEstate.Manager.ObjectModel.Costcalculate
{
    public class ItemFeeAutoCreateTask : MultiTask
    {
        public override void LoadTask()
        {
            TaskContent task = NewObject<TaskContent>("restask");
            TimingTask timing = new TimingTask();
            timing.TimingTaskExcuter = task;
            timing.TimingTaskType = TimingTaskType.PerDay;
            timing.ExcuteTime = new ShortTime(1, 1, 1);
            taskList.Add(timing);
        }
    }

    public class ResRateTaskContent : TaskContent
    {
        private CashManagerDao cashDao = null;

        public ResRateTaskContent()
        {
        }

        public override void InitDao()
        {
            base.InitDao();
            cashDao = NewDao<CashManagerDao>();
        }

        public override void ExcuteOnTime(DateTime date)
        {
            AbstractOSRCCItemRelation osrcc = NewObject<AbstractOSRCCItemRelation>();
            AbstractItemFeeHead head = NewObject<AbstractItemFeeHead>();
            AbstractItemFeeOrder feeorder = NewObject<AbstractItemFeeOrder>();

            //1.获取需要产生费用的项目
            DataTable dt = cashDao.GetSrccitemData();
            //2.循环判断并生成费用
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DateTime Thismaturity = Convert.ToDateTime(dt.Rows[i]["Thismaturity"]);
                DateTime CurrentDate = Thismaturity.AddMonths(1);//事后交，本次交上个月的物业费
                string SrccId = dt.Rows[i]["SrccId"].ToString();
                string ItemCode = dt.Rows[i]["ItemCode"].ToString();
                string ItemName = dt.Rows[i]["ItemName"].ToString();
                decimal price=Convert.ToDecimal(dt.Rows[i]["Price"]);
                string StatCode = dt.Rows[i]["StatCode"].ToString();
                DataRow dr = cashDao.GetSrccInfo(SrccId);

                while (DateTime.Now.Date >= CurrentDate.Date)
                {
                    AbstractResRateTime res = NewObject<AbstractResRateTime>("restime");
                    res.LastDate = Thismaturity.Date;
                    res.CurrentDate = CurrentDate.Date;
                    res.Unitprice = price;
                    res.Usearea = Convert.ToDecimal(dr["UseArea"]);
                    List<object[]> segvalue;
                    decimal ThisCostFee = res.GetItemFee(out segvalue);
                    decimal ThisLateFee = res.GetLateFee();
                    //更新OSRCCItem显示数据
                    osrcc.UpdateThisFee(SrccId, ItemCode, CurrentDate.Date, ThisCostFee, ThisLateFee);

                    for (int k = 0; k < segvalue.Count; k++)
                    {
                        AbstractItemFeeHead _head = (AbstractItemFeeHead)head.Clone();
                        _head.OwnerId = dr["OwnerId"].ToString() == "" ? null : dr["OwnerId"].ToString();
                        _head.OwnerName = dr["OwnerName"].ToString();
                        _head.SrccId = SrccId;
                        _head.ItemCode = ItemCode;
                        _head.ItemName = ItemName;
                        _head.Lastmaturity = Convert.ToDateTime(segvalue[k][0]);
                        _head.Thismaturity = Convert.ToDateTime(segvalue[k][1]);
                        _head.Price = price;
                        _head.DosageNum = 1;
                        _head.CostFee = Convert.ToDecimal(segvalue[k][2]);
                        _head.LateFee = 0;//?
                        _head.save();
                        List<object[]> dayvalue = (List<object[]>)segvalue[k][3];
                        for (int j = 0; j < dayvalue.Count; j++)
                        {
                            AbstractItemFeeOrder _feeorder = (AbstractItemFeeOrder)feeorder.Clone();
                            _feeorder.OrderID = null;
                            _feeorder.HeadId = _head.HeadId;
                            _feeorder.OwnerId = dr["OwnerId"].ToString() == "" ? null : dr["OwnerId"].ToString();
                            _feeorder.OwnerName = dr["OwnerName"].ToString();
                            _feeorder.SrccId = SrccId;
                            _feeorder.ItemCode = ItemCode;
                            _feeorder.ItemName = ItemName;
                            _feeorder.StatCode = StatCode;
                            _feeorder.OrderDate = Convert.ToDateTime(dayvalue[j][0]);
                            _feeorder.TotalFee = Convert.ToDecimal(dayvalue[j][1]);
                            _feeorder.Save();
                        }
                    }

                    CurrentDate = CurrentDate.AddMonths(1);
                }
            }
            //3.重新计算滞纳金
        }
    }

    public class StopCarRateTaskContent : TaskContent
    {
        public override void ExcuteOnTime(DateTime dt)
        {
            throw new NotImplementedException();
        }
    }


}
